package org.dromara.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.domain.AuditDto;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpFarmSale;
import org.dromara.erp.domain.ErpFarmSaleInfo;
import org.dromara.erp.mapper.ErpFarmSaleInfoMapper;
import org.dromara.erp.mapper.ErpFarmSaleMapper;
import org.dromara.erp.service.IErpFarmSaleService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 农场销售Service业务层处理
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-16
 */
@RequiredArgsConstructor
@Service
public class ErpFarmSaleServiceImpl implements IErpFarmSaleService {

    private final ErpFarmSaleMapper baseMapper;
    private final ErpFarmSaleInfoMapper infoMapper;

    /**
     * 查询农场销售
     */
    @Override
    public ErpFarmSale queryById(Long id){
        //查询主表
        ErpFarmSale sale = baseMapper.selectById(id);
        //查询子表
        LambdaQueryWrapper<ErpFarmSaleInfo> infoWrapper = new LambdaQueryWrapper<>();
        infoWrapper.eq(ErpFarmSaleInfo::getOrderId, id);
        List<ErpFarmSaleInfo> infos = infoMapper.selectList(infoWrapper);
        sale.setContentInfo(infos);
        return sale;
    }

    /**
     * 查询农场销售列表
     */
    @Override
    public TableDataInfo<ErpFarmSale> queryPageList(ErpFarmSale bo, PageQuery pageQuery) {
        Page<ErpFarmSale> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询农场销售列表
     */
    @Override
    public List<ErpFarmSale> queryList(ErpFarmSale bo) {
        LambdaQueryWrapper<ErpFarmSale> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ErpFarmSale> buildQueryWrapper(ErpFarmSale bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpFarmSale> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), ErpFarmSale::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getBuyer()), ErpFarmSale::getBuyer, bo.getBuyer());
        lqw.eq(StringUtils.isNotBlank(bo.getBuyerTel()), ErpFarmSale::getBuyerTel, bo.getBuyerTel());
        lqw.eq(bo.getWorkerId() != null, ErpFarmSale::getWorkerId, bo.getWorkerId());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPayment()), ErpFarmSale::getIsPayment, bo.getIsPayment());
        lqw.eq(StringUtils.isNotBlank(bo.getIsCommission()), ErpFarmSale::getIsCommission, bo.getIsCommission());
        lqw.eq(bo.getSaleTime() != null, ErpFarmSale::getSaleTime, bo.getSaleTime());
        return lqw;
    }

    /**
     * 新增农场销售
     */
    @Override
    public Boolean insertByBo(ErpFarmSale bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            //增加成功后，主表返回ID
            bo.setId(bo.getId());
            //获取子表
            List<ErpFarmSaleInfo> info = bo.getContentInfo();
            //遍历出子表数据
            info.forEach(x->{
                //设置关联ID
                x.setOrderId(bo.getId());
                //执行子表新增方法
                infoMapper.insert(x);
            });
        }
        return flag;
    }

    /**
     * 修改农场销售
     */
    @Override
    public Boolean updateByBo(ErpFarmSale bo) {
        validEntityBeforeSave(bo);
        //获取子表
        List<ErpFarmSaleInfo> infoList = bo.getContentInfo();
        //遍历子表数据
        infoList.forEach(x->{
            x.setOrderId(bo.getId());
            //如果有新增则执行新增的方法
            if(x.getId() ==null){
                infoMapper.insert(x);
            }
            //执行子表更新方法
            infoMapper.updateById(x);
        });
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpFarmSale entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除农场销售
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    /*标题*/
    @Override
    public String defaultTitle(String formatDate) {
        Integer totalNum = baseMapper.selectByDateCunt(formatDate);
        //最低3位数，不足前面用0填充
        //拼接成新的标题
        return String.format("%03d", totalNum);
    }
    /*审核*/
    @Override
    public void updateAudit(AuditDto dto) {
        LambdaUpdateWrapper<ErpFarmSale> updateWrapper = new LambdaUpdateWrapper<>();
        //设置更新条件
        updateWrapper.eq(ErpFarmSale::getId,dto.getId());
        //更新字段
        updateWrapper.set(ErpFarmSale::getAuditStatus,dto.getAuditStatus());
        updateWrapper.set(ErpFarmSale::getAuditRemark,dto.getAuditRemark());
        updateWrapper.set(ErpFarmSale::getAuditTime,new Date());

    }
}
